package com.neusoft.actor.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.neusoft.actor.client.ClientCareItemClient;
import com.neusoft.actor.dao.CareRecordDao;
import com.neusoft.actor.dao.CaretakerClientDao;
//import com.neusoft.actor.dao.ClientCareItemDao;
import com.neusoft.actor.dao.ClientDao;
import com.neusoft.actor.po.*;
import com.neusoft.actor.service.CareRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CareRecordServiceImpl implements CareRecordService {

    @Autowired
    private CareRecordDao careRecordDao;

    @Autowired
    private CaretakerClientDao caretakerClientDao;

    @Autowired
    private ClientDao clientDao;

//    @Autowired
//    private ClientCareItemDao clientCareItemDao;

    @Autowired
    private ClientCareItemClient clientCareItemClient;

    @Override
    public ResponseBean<List<CareRecord>> getAllCareRecords() {
        List<CareRecord> careRecords = careRecordDao.selectList(null);
        return new ResponseBean<>(careRecords);
    }

    @Override
    public ResponseBean<CareRecord> getCareRecordById(Integer recordId) {
        return null;
    }

    @Override
//    根据客户id查询护理信息记录
    public ResponseBean<List<CareRecord>> getCareRecordByClientId(Integer clientId) {
        List<CareRecord> careRecords = careRecordDao.selectList(new QueryWrapper<CareRecord>().eq("client_id", clientId).eq("status",""));
        if (careRecords != null) {
            return new ResponseBean<>(careRecords);
        } else {
            return new ResponseBean<>(404, "护理记录未找到");
        }
    }

    @Override
//健康管家查询自己客户的护理记录,可以根据姓名模糊查询
    public ResponseBean<List<CareRecord>> getCareRecordByCareTakerId(Integer careTakerId, String clientName) {
        // 查询健康管家管理的所有客户ID
        List<CaretakerClient> caretakerClients = caretakerClientDao.selectList(new QueryWrapper<CaretakerClient>().eq("caretaker_id", careTakerId));
        List<Integer> clientIds = caretakerClients.stream()
                .map(CaretakerClient::getClientId)
                .collect(Collectors.toList());

        // 如果没有客户，直接返回404
        if (clientIds.isEmpty()) {
            return new ResponseBean<>(404, "该健康管家没有客户");
        }

        // 查询所有客户的护理记录
        List<CareRecord> allCareRecords = new ArrayList<>();
        for (Integer clientId : clientIds) {
            List<CareRecord> careRecords = careRecordDao.selectList(new QueryWrapper<CareRecord>().eq("client_id", clientId));
            allCareRecords.addAll(careRecords);
        }

        // 如果提供了客户名称，则进行模糊查询
        if (clientName != null && !clientName.isEmpty()) {
            allCareRecords = allCareRecords.stream()
                    .filter(record -> {
                        Client client = clientDao.selectById(record.getClientId());
                        return client != null && client.getClientName().contains(clientName);
                    })
                    .collect(Collectors.toList());
        }

        // 返回结果
        if (!allCareRecords.isEmpty()) {
            return new ResponseBean<>(allCareRecords);
        } else {
            return new ResponseBean<>(404, "未找到符合条件的护理记录");
        }
    }

    @Override
//    添加护理记录，同时更新护理记录数量
    public ResponseBean<Integer> addCareRecord(CareRecord careRecord) {
        int result = careRecordDao.insert(careRecord);
        ClientCareItem clientCareItem = clientCareItemClient.getClientCareItem(careRecord.getClientId(), careRecord.getItemId()).getData();
        if (clientCareItem.getQuantity() >= careRecord.getQuantity()) {
            clientCareItem.setQuantity(clientCareItem.getQuantity() - careRecord.getQuantity());
            System.out.println(clientCareItem.getClientItemId());
            clientCareItemClient.updateById(clientCareItem);
        } else {
            return new ResponseBean<>(500, "护理项目数量不足，无法添加");
        }
        return new ResponseBean<>(result); // 返回插入成功的行数
    }

    @Override
    public ResponseBean<Integer> updateCareRecord(CareRecord careRecord) {
        int result = careRecordDao.updateById(careRecord);
        return new ResponseBean<>(result); // 返回更新成功的行数
    }

    @Override
//    护理记录逻辑删除
    public ResponseBean<Integer> deleteCareRecord(Integer recordId) {
        CareRecord careRecord = careRecordDao.selectById(recordId);
        careRecord.setStatus("移除");
        int result = careRecordDao.updateById(careRecord);
        return new ResponseBean<>(result); // 返回删除成功的行数
    }
}